import Vue from "vue";
import Vuex from 'vuex'

Vue.use(Vuex)

export default function () {
  return new Vuex.Store({
    state: {
      name: ''
    },
    mutations: {
      setName(state, data) {
        console.log('mutations:', data)
        state.name = data
      }
    },
    actions: {
      getName({ commit }, payload) {
        console.log('actions:', payload)
        return new Promise(resolve => {
          setTimeout(() => {
            resolve(payload.data)
          }, 2000)
        })
          .then((res) => {
            console.log('then:', res)
            commit('setName', res)
          })
      }
    }
  })
}